package com.sunfeng.chap2.p13;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @Author : 孙峰
 * @Description:  如果调用  FileLock lock(long position, long size, boolean shared) 方法时已经设置了调用方的中断状态，则立即抛出该异常; 不更改线程的中断状态
 * @Date : 2021/10/9  16:17
 */
public class Test13_4 {
    public static void main(String[] args) throws IOException, InterruptedException {
        FileOutputStream fileOutputStream = new FileOutputStream(new File("d:\\nio\\a.txt"));
        final FileChannel fileChannelA = fileOutputStream.getChannel();

        Thread t1 = new Thread() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 1000000; i++) {
                        System.out.println("i=" + (i + 1));
                    }

                    fileChannelA.lock(1, 2, false);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t1.start();
        Thread.sleep(50);
        t1.interrupt(); // 先执行中断方法
        Thread.sleep(30000);
        fileChannelA.close();
        fileOutputStream.close();

    }
}
